import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js'
import XInput from 'vux/src/components/x-input/index.vue'
import Group from 'vux/src/components/group/index.vue'
import Datetime from 'vux/src/components/datetime/index.vue'
import Picker from 'vux/src/components/picker/index.vue'
import Popup from 'vux/src/components/popup/index.vue'

var siteIda = window.sessionStorage.getItem('siteId')
var list = []
http.post(CONSTANT.DICTLIST, {
  orgId: siteIda,
  type: 'edu_grade'
}, (res) => {
  // 获取年级段
  list.push({
  name: '请选择年级',
  value: '-1'})
res.data.forEach(item => {
  var listOne = {}
  listOne.name = item.label
listOne.value = item.value
list.push(listOne)
})
})
export default {
  name: 'regist',
  components: {XInput, Group, Datetime, Picker, Popup},
  data () {
    return {
      minYear: 1900,
      maxYear: 2017,
      endDate: '2017-12-21',
      show: false,
      gradeList: [list],
      gradeListValue: [],
      siteId: '',
      school: '',
      studentId: '',
      idCardNeeds: true,
      name: '',
      mobile: '',
      numID: '',
      classNum: '',
      birthday: '',
      sex: '男',
      idplaceholder: '请输入身份证',
      sfzyanzhen: function (value) {
        let r = /^(\d{18}|\d{17}x)$/i;
        if (r.test(value)) {
          return {
            valid: true,
            msg: '身份证正确'
          }
        } else {
          return {
            valid: false,
            msg: '身份证错误'
          }
        }
      }
    }
  },
  mounted () {
    var _this = this
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    _this.maxYear = year
    _this.endDate = year + '-' + month + '-' + day
    _this.siteId = window.sessionStorage.getItem('siteId')
    _this.studentId = window.sessionStorage.getItem('studentId')
    http.post(CONSTANT.SETTING, {
      orgId: _this.siteId
    }, (res) => {
      // 获取身份证是否是必填项
      let setting = res.data.initData.setting
      _this.idCardNeeds = setting.idCardNeed
    window.sessionStorage.setItem('isStudBack', setting.isStudBack)
    if (_this.idCardNeeds == false) {
      _this.idplaceholder = '请输入孩子身份证（选填）'
    } else {
      _this.idplaceholder = '请输入孩子身份证'
    }
  })
  },
  methods: {
    overModify () {
      var _this = this
      var sexs = 0
      if (_this.sex == '男') {
        sexs = 0
      } else {
        sexs = 1
      }
      if (_this.name == '') {
        alert('请输入您的姓名！')
      } else if (_this.mobile == '') {
        alert('请输入您的手机号码！')
      } else if (_this.$refs.tel.valid == false) {
        alert('请输入正确的手机号码！')
      } else if (_this.numID == '' && _this.idCardNeeds == true) {
        alert('请输入您的身份证号码！')
      } else if (_this.$refs.idcard.valid == false) {
        alert('请输入正确的身份证号码！')
      } else if (_this.birthday == '') {
        alert('请选择出生日期！')
      } else if (_this.gradeListValue[0] == '-1') {
        alert('请选择年级！')
      } else {
        http.post(CONSTANT.CHECKIDCARDEXIST, {
          orgId: _this.siteId,
          name: _this.name,
          mobile: _this.mobile,
          cardNo: _this.numID,
          grade: _this.gradeListValue[0],
          sex: sexs,
          birthday: _this.birthday,
          school: _this.school
        }, (res) => {
          console.log(res)
        if (res.data.isExisting) {
          alert('身份证号码已存在')
        } else {
          http.post(CONSTANT.CHECKEXIST, {
            orgId: _this.siteId,
            name: _this.name,
            mobile: _this.mobile,
            cardNo: _this.numID,
            grade: _this.gradeListValue[0],
            sex: sexs,
            birthday: _this.birthday,
            school: _this.school
          }, (res) => {
            console.log(res)
          if (res.data.isExisting) {
            alert('账号已存在')
          } else {
            http.post(CONSTANT.REGISTER, {
              orgId: _this.siteId,
              name: _this.name,
              mobile: _this.mobile,
              cardNo: _this.numID,
              grade: _this.gradeListValue[0],
              sex: sexs,
              birthday: _this.birthday,
              school: _this.school
            }, (res) => {
              console.log(res)
            if (res.code != 0) {
              alert(res.msg)
            } else {
              alert('注册成功！')
              _this.$router.push({path: '/login'})
            }
          })
          }
        })
        }
      })
      }
    },
    chooseOK () {
      var _this = this
      _this.show = false
    },
    checkone (e) {
      // 男选择按钮
      var _this = this
      if (e.srcElement.className == 'iconfont icon-danxuan-xuanzhong iconfontChecked') {
        e.target.parentElement.children[1].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '男'
      } else {
        e.srcElement.className = 'iconfont icon-danxuan-xuanzhong iconfontChecked'
        e.target.parentElement.children[1].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '男'
      }
      console.log(this.sex)
    },
    checktwo (e) {
      // 女选择按钮
      var _this = this
      if (e.srcElement.className == 'iconfont icon-danxuan-xuanzhong iconfontChecked') {
        e.target.parentElement.children[0].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '女'
      } else {
        e.srcElement.className = 'iconfont icon-danxuan-xuanzhong iconfontChecked'
        e.target.parentElement.children[0].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '女'
      }
      console.log(this.sex)
    },
    comeBack () {
      window.history.go(-1)
    }
  }
}
